<!-- 引入布局指令的命名空间 -->
<#import "common/layout.html" as layout>
<@layout.layout>
    <div id="register">
        <h1>注册</h1>
        <form method="post" action="/user/do_register" id="register_form">
            <table class="table table-bordered table-hover table-condensed">
                <tr>
                    <td>手机号：</td>
                    <td><input type="text" class="form-control" name="phone"/></td>
                </tr>

                <tr>
                    <td>邮箱：</td>
                    <td><input type="text" class="form-control" name="email"/></td>
                </tr>

                <tr>
                    <td>密码：</td>
                    <td><input type="password" class="form-control" name="password"/></td>
                </tr>

                <tr>
                    <td>确认密码：</td>
                    <td><input type="password" class="form-control" name="password2" data-rule="required;match(password)"/></td>
                </tr>

                <tr>
                    <td>姓名：</td>
                    <td><input type="text" class="form-control" name="username" data-rule="required"/></td>
                </tr>
            </table>
            <button type="submit" class="btn btn-primary form-control">注册</button>
        </form>
    </div>

    <script>
        var fromUrl = "${fromUrl}";

        $('#register_form').validator({
            fields: {
                'phone':'required;mobile',
                'email': 'required;email',
                'password': 'required;password',
                'password2': 'required;match(password)',
                'username': 'required',
            },
            valid: function(form) {
                var $formObj = $(form);
                $.ajax({
                    type: "POST",
                    url: $formObj.prop("action"),
                    data: $formObj.serialize(),
                    success: function(dataJson) {
                        if(dataJson.statusCode == 200){
                            alert("注册成功");
                            window.location.href = "/user/login?fromUrl="+fromUrl;//重定向跳转
                        }else{
                            alert(dataJson.message);
                        }
                    },
                    error: function(errorMsg){
                        alert("系统异常");
                    }
                });
            }
        });
    </script>
</@layout.layout>